﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

using FulcrumApi.clients;
using FulcrumApi.entities;
using FulcrumApi.helpers;

namespace FulcrumApi.services
{
    public class ProjectServices
    {
    	FulcrumHttpClient client;
    	
    	public ProjectServices(FulcrumHttpClient client)
    	{
    		this.client = client;
    	}
    	
    	public List<Project> ListProjects() {
			try {
				Projects projects = ParseObject.fromXml<Projects>(client.doGet("/projects.xml", new Dictionary<String, String>()));
				return projects.Project;			
			} catch (Exception e) {
	            if (e.InnerException.Message.Equals("<nil-classes xmlns=''> n'était pas attendu."))
	            {
					return new List<Project>();
				}
				throw;
			}
		}
	
		public Project GetProject(int i) {
			return ParseObject.fromXml<Project>(client.doGet("/projects/" + i + ".xml", new Dictionary<String, String>()));
		}
		
		public Project Create(String name, String pointScale, int defaultVelocity, DateTime startDate, int iterationStartDay, int iterationLength) {
			
		    DateTime now = DateTime.Now;
			Dictionary<String, String> parameters = new Dictionary<String, String>();
			parameters.Add("project[name]", name);
			parameters.Add("project[point_scale]", pointScale);
			parameters.Add("project[default_velocity]", defaultVelocity.ToString());
			parameters.Add("project[start_date(1i)]", startDate.Year.ToString());
			parameters.Add("project[start_date(2i)]", startDate.Month.ToString());
			parameters.Add("project[start_date(3i)]", startDate.Day.ToString());
			parameters.Add("project[iteration_start_day]", iterationStartDay.ToString());
			parameters.Add("project[iteration_length]", iterationLength.ToString());
			parameters.Add("commit", "Create Project");
			Stream res = client.doPost("/projects.xml", parameters);
			return ParseObject.fromXml<Project>(res);
		}
		
		public void Delete(Project project) {
			client.doDelete(String.Format("/projects/{0}", project.Id));
		}
	
		public void Update(Project createdProject) {
			var parameters = new Dictionary<String, String>();
			parameters.Add("_method", "put");
			parameters.Add("project[name]", createdProject.Name);
			parameters.Add("project[point_scale]", createdProject.PointScale);
			parameters.Add("project[default_velocity]", createdProject.DefaultVelocity.ToString());
			parameters.Add("project[start_date(1i)]", createdProject.StartDate.Year.ToString());
			parameters.Add("project[start_date(2i)]", createdProject.StartDate.Month.ToString());
			parameters.Add("project[start_date(3i)]", createdProject.StartDate.Day.ToString());
			parameters.Add("project[iteration_start_day]", createdProject.IterationStartDay.ToString());
			parameters.Add("project[iteration_length]", createdProject.IterationLength.ToString());
			parameters.Add("commit", "Update Project");
			client.doPost(String.Format("/projects/{0}.xml", createdProject.Id), parameters);
			
		}
    	
    }
}
